package com.hdit.guimei.util;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.apache.logging.log4j.core.tools.Generate;

import javax.print.DocFlavor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

/**
 * @author 冰镇小可乐
 * @ClassNameGenerateOrderNumUtil
 * @Description TODO
 * @Date 2021/1/25 10:46
 * Version 1.0
 */
public class GenerateOrderNumUtil {
    //全局自增数
    private static int count=0;
    //设置每毫秒可以生成多少订单
    private  static final int TOTAL=999;
    //格式化的时间字符串
    private static  final SimpleDateFormat SDF =new SimpleDateFormat("yyyyMMddHHmmssSS");
    //获取当前时间年月日分秒毫秒字符串
    private static String getNowDateStr(){
        return SDF.format(new Date());
    }
    //记录上一次的时间，用来判断是否需要递增全局数
    private  static String now=null;

    public synchronized static String GenerateOrder(){
        String dateStr=getNowDateStr();
        if (dateStr.equals(now)){
            count++;
        }else {
            count=1;
            now=dateStr;
        }
        //计算补位
        int countInteget=String.valueOf(TOTAL).length()- String.valueOf(count).length();
        String bu="";//补字符串
        for (int i=0;i<countInteget;i++){
            bu+=0;
        }
        bu+=String.valueOf(count);
        if (count>=TOTAL){
            count=0;
        }
        return dateStr+bu;
    }
}
